package controller;

import entity.Address;
import entity.User;
import org.apache.commons.beanutils.BeanUtils;
import service.AddressService;
import service.impl.AddressServiceImpl;
import util.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author: xxf
 * @CreateDate: 2020/10/7 17:02
 */
@WebServlet("/address")
public class AddressController extends BaseController {

    private AddressService service = new AddressServiceImpl();

    /**
     * 展示收货地址信息
     * @param request
     * @param response
     * @return
     */
    public String show(HttpServletRequest request, HttpServletResponse response) {

        //获取用户登录的user对象
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            //没登录
            request.setAttribute("msg", "请先登录您的账号");
            return Constants.REDIRECT + "/login.jsp";
        }
        //已经登录
        int uid = loginUser.getUid();

        //调用service层
        List<Address> list = service.findAddressByUid(uid);

        //响应
        request.setAttribute("list", list);

        return Constants.FORWARD + "/self_info.jsp";
    }
    /**
     * 添加收货地址信息
     * @param request
     * @param response
     * @return
     */
    public String add(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {

        //获取请求参数
        Map<String, String[]> map = request.getParameterMap();
        Address address = new Address();
        BeanUtils.populate(address, map);


        //调用service层
        service.saveAddress(address);

        return Constants.FORWARD + "/address?method=show";
    }
    /**
     * 删除收货地址信息
     * @param request
     * @param response
     * @return
     */
    public String delAddress(HttpServletRequest request, HttpServletResponse response) {

        //获取请求参数
        String aid = request.getParameter("aid");


        //调用service层
        service.delAddressByAid(Integer.parseInt(aid));

        return Constants.FORWARD + "/address?method=show";
    }
    /**
     * 设置默认收货地址
     * @param request
     * @param response
     * @return
     */
    public String setDefaultAddress(HttpServletRequest request, HttpServletResponse response)  {

        //获取请求参数
        String aid = request.getParameter("aid");
        //获取用户登录的user对象
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            //没登录
            request.setAttribute("msg", "请先登录您的账号");
            return Constants.REDIRECT + "/login.jsp";
        }
        //已经登录
        int uid = loginUser.getUid();

        //调用service层
        service.setAddressToDefault(Integer.parseInt(aid),uid);

        return Constants.FORWARD + "/address?method=show";
    }
    /**
     * 修改收货地址信息
     * @param request
     * @param response
     * @return
     */
    public String updateAddress(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {

        //获取请求参数
        Map<String, String[]> map = request.getParameterMap();
        Address address = new Address();
        BeanUtils.populate(address, map);

        //调用service层
        service.updateAddressByAid(address);

        return Constants.FORWARD + "/address?method=show";
    }


}
